require "gadgets/listbox"
module EventEditor
  module Gadgets
    class SwitchListDialog
      LIST_ITEM_COUNT = 15
      def self.show(base, switches, &func)
        @dialog = Gtk::Dialog.new("Switch Selection",
               base,
               Gtk::Dialog::MODAL |
               Gtk::Dialog::DESTROY_WITH_PARENT,
  			       ["OK", Gtk::Dialog::RESPONSE_OK],
  			       ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )
        @dialog.window_position = Gtk::Window::POS_MOUSE
        @func = func
        @switches = switches.clone

        # packing
        hbox = Gtk::HBox.new(true, 10)
        hbox.set_border_width(5)
        
        hbox.add(self.create_navigation_panel)
        hbox.add(self.create_switch_panel)
        
        @dialog.vbox.add(hbox)
        @dialog.set_size_request(400, 500)
        @dialog.vbox.show_all

        self.refresh
        
        @dialog.run do |response|
          if response == Gtk::Dialog::RESPONSE_OK
            no = (selected_index == -1) ? 0 : selected_index
            func.call(@switches, no)
          else
            func.call(:cancel, nil)
          end
          @dialog.destroy
        end
      end
      
      def self.create_switch_panel
        vbox = Gtk::VBox.new(false, 5)
        @switch_listbox = ::Gadgets::Listbox.new("Switches", [String, String])
        hbox = Gtk::HBox.new(false, 10)
        vbox.add(@switch_listbox)
        vbox.pack_start(self.create_name_set_panel, false, false)
        return vbox
      end
      
      def self.create_navigation_panel
        vbox = Gtk::VBox.new(false, 5)
        @navigation_listbox = ::Gadgets::Listbox.new("Navigation", [String, String]) do
          self.refresh_list
        end
        
        vbox.add(@navigation_listbox)
        vbox.pack_start(self.create_max_number_panel, false, false)
        return vbox
      end
      
      def self.create_name_set_panel
        hbox = Gtk::HBox.new(false, 5)
        @name_input = Gtk::Entry.new
        b = Gtk::Button.new("Set")
        b.signal_connect('button-press-event') do 
          self.set_name(@name_input.text)
        end
        hbox.pack_start(Gtk::Label.new("Name"), false, false)
        n = @name_input
        n.set_width_request(100)
        hbox.pack_start(n, false, false)
        hbox.pack_start(b, false, false)
        return hbox
      end
      
      def self.create_max_number_panel
        hbox = Gtk::HBox.new(false, 5)
        @maximum_number_input = Gtk::SpinButton.new(1, 9999, 1)
        @maximum_number_input.value = @switches.length
        b = Gtk::Button.new("Set")
        b.signal_connect('button-press-event') do 
          self.set_max_number(@maximum_number_input.value)
        end
        hbox.pack_start(Gtk::Label.new("Max"), false, false)
        m = @maximum_number_input
        m.set_width_request(60)
        hbox.pack_start(m, false, false)
        hbox.pack_start(b, false, false)
        return hbox
      end
      
      def self.selected_index
        return @navigation_listbox.selected_data.to_i * LIST_ITEM_COUNT + @switch_listbox.selected_index
      end
      
      def self.set_name(name)
        if @switch_listbox.selected_item
          t = self.selected_index
          @switch_listbox.selected_item[0] = "#{t}: " + name
          @switches[t] = name
        end
      end
      
      def self.set_max_number(num)
        arr = Array.new(num, '')
        arr[0, [@switches.length, arr.length].min] = @switches[0..([@switches.length, arr.length].min - 1)]
        @switches = arr
        self.refresh
      end
      
      def self.refresh
        @navigation_listbox.clear
        (@switches.length / LIST_ITEM_COUNT.to_f).ceil.times do |i|
          st = i * LIST_ITEM_COUNT
          en = st + LIST_ITEM_COUNT
          @navigation_listbox.add_item("[#{st} - #{en}]", i.to_s)
        end
        @navigation_listbox.selected_index = 0
      
        self.refresh_list
      end
      
      def self.refresh_list
        @switch_listbox.clear
        
        arr = []
        st = @navigation_listbox.selected_data.to_i * LIST_ITEM_COUNT
        en = st + LIST_ITEM_COUNT
        (st..en).each do |i|
          break if i >= @switches.length
          arr << ["#{i}: " + @switches[i].to_s, @switches[i].to_s]
        end
        
        @switch_listbox.set_data(arr)
      end
    end
  end
end